package generadorDescriptores;

import java.util.ArrayList;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import generadorDescriptores.dom.XmlFile;

public class FunctionsXML {
	
	private static Document document;
	private static XmlFile xmlfile; 
	//private static String path = ".\\anotherFiles\\functions.xml"; 
	private static String path = "xml/functions.xml";
	
	public static void loadXml(){
		xmlfile = new XmlFile(path);
		xmlfile.load();
		document = xmlfile.getDocument();
	}
	
	public static void addFunction(String function){
		loadXml();
		Element newNode = createElementFunction(function);
		NodeList list = document.getElementsByTagName("functions");
		list.item(0).appendChild(newNode);		
		XmlFile.serialize(document, path);
	}
	
	private static Element createElementFunction(String function){
		Element newFunction = document.createElement("function");
		Node functionName = document.createTextNode(function);
		newFunction.appendChild(functionName);
		return newFunction;
	}
	

	public static Document getDocument() {
		return document;
	}
	
	public static ArrayList<String> getFunctions(){
		loadXml();
		ArrayList<String> list = new ArrayList<String>();
		NodeList nodelist = document.getElementsByTagName("function");
		for(int i=0; i<nodelist.getLength(); i++){
			list.add(nodelist.item(i).getTextContent());
		}
		return list;		
	}	

}
